from common.setting import ensure_path_sep
from third_services.allegro.RebatyesAndPromotions.offerdetails import OfferDetails
from utils.mysqlUtils.db_pool import Database


class SycLoseCategoryTestCase:
    OfferDetailsEex = OfferDetails()

    def test_syc_lose_category(self):
        db_java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
        insert_sql = """
        INSERT INTO monitor.categories 
        (category_id,is_leaf,name,parent_id,redirect_category)
        VALUES (%s,%s,%s,%s,%s);
        """
        from_ = None
        while True:
            data_list = self.OfferDetailsEex.get_sale_cayegory_enents(11412, from_, 100)

            data_list = data_list.get('events')
            if not data_list:
                break
            from_ = data_list[-1].get('id')

            # category_id,is_leaf,name,parent_id,redirect_category
            insert_data = list(map(lambda x: (
            x.get('category').get('id'),
            1 if x.get('category').get('leaf') else 0,
            x.get('category').get('name'),
            x.get('category').get('parent').get('id'),
            x.get('redirectCategory').get('id')),
                                   data_list))

            rows = db_java_test.execute_many(insert_sql, list(insert_data))
            print(f"插入了{rows}")


if __name__ == '__main__':
    SycLoseCategoryTestCase().test_syc_lose_category()
